package com.example.aqarapp.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import aqarapp.R;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.aqarapp.entity.BaseDeviceInfo;
import com.example.aqarapp.entity.BulbInfo;
import com.example.aqarapp.entity.CurtainInfo;
import com.example.aqarapp.entity.DeviceInfo;
import com.example.aqarapp.view.adapter.BulbScenesRecyclerAdapter;
import com.example.aqarapp.view.AverageGapItemDecoration;
import com.example.aqarapp.view.utils.Assert;

import java.text.NumberFormat;
import java.util.ArrayList;

import static com.example.aqarapp.view.utils.MathUtils.getPercentageFromString;
import static com.example.aqarapp.view.utils.MathUtils.transformColor;

public class BulbControlDialog extends Dialog implements View.OnClickListener {
    private static final String TAG = "BULB_CONTROL_DIALOG";
    private DeviceInfo mDeviceInfo;
    private BaseQuickAdapter<DeviceInfo, ?> mAdapter;
    private int mPosition;
    private RecyclerView mRrecyclerView;
    private BulbScenesRecyclerAdapter myAdapter;
    private ArrayList<String> dataList;
    private Context mContext;
    private Point mWindowSize = new Point();
    private BulbInfo mBulbInfo;
    private ToggleButton switchButton;
    private SeekBar lightSeekBar;
    private SeekBar colorSeekBar;


    public BulbControlDialog(@NonNull Context context, DeviceInfo deviceInfo, @NonNull BaseQuickAdapter<DeviceInfo, ?> adapter, int position) {
        super(context, R.style.BulbControlDialog);
        mContext = context;
        mDeviceInfo =deviceInfo;
        mBulbInfo = (BulbInfo) deviceInfo.getBaseDeviceInfo();
        mAdapter = adapter;
        mPosition = position;
    }

    public BulbControlDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected BulbControlDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_bulb_inf_dialog);
        initView();
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        display.getSize(mWindowSize);
        lp.width = mWindowSize.x * 4 / 5;// 设置dialog宽度为屏幕的4/5
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);//点击外部Dialog消失
        initBulbScenesView(); //todo 需要确定灯泡的场景如何获得。
    }

    private void initView() {
        switchButton = findViewById(R.id.bulb_switch_tb);
        lightSeekBar = findViewById(R.id.bulb_light_seekbar);
        colorSeekBar = findViewById(R.id.bulb_color_seekbar);
        switchButton.setChecked( ((BulbInfo)mDeviceInfo.getBaseDeviceInfo()).getBulbStatus().equals("1"));
        Log.d("liyao1333","mBulbInfo.colortemperature="+mBulbInfo.colortemperature);
        Assert.that(rangeInDefined(Integer.parseInt(mBulbInfo.colortemperature), 175, 370), "bulb color temperature value not in correct range");
        colorSeekBar.setProgress(Integer.parseInt(mBulbInfo.colortemperature));//初始化灯泡色温值
        Assert.that(rangeInDefined(Integer.parseInt(mBulbInfo.bulbLight), 0, 100), "bulb light value not in correct range");
        lightSeekBar.setProgress(Integer.parseInt(mBulbInfo.bulbLight));//初始化灯泡亮度值

        //绑定监听
        switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                //todo 1.调远程接口开关灯泡 如果执行失败下面代码不执行
                ((BulbInfo)mDeviceInfo.getBaseDeviceInfo()).setBulbStatus((isChecked ?  "1": "0")) ;
                mDeviceInfo.setBaseDeviceInfo(mBulbInfo);
                //2.同步cardView UI
                mAdapter.removeAt(mPosition);
                mAdapter.addData(mPosition,mDeviceInfo);
                mAdapter.notifyItemChanged(mPosition);
            }
        });
        lightSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int lightProgress, boolean b) {
                mBulbInfo.bulbLight = NumberFormat.getInstance().format(lightProgress);
                mDeviceInfo.setBaseDeviceInfo(mBulbInfo);
                //  mBulbInfo.bulbStatus = NumberFormat.getInstance().format(lightProgress);
                //todo 1.调远程接口 修改亮度值
                //2.同步cardView UI
                mAdapter.removeAt(mPosition);
                mAdapter.addData(mPosition, mDeviceInfo);
                mAdapter.notifyItemChanged(mPosition);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        colorSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int colorTemperature, boolean b) {
//                int colorTemperature1 = transformColor(colorTemperature);
                Log.d("liyao1333","mBulbInfo.colorTemperature="+colorTemperature);
                mBulbInfo.colortemperature = String.valueOf(colorTemperature);
                mDeviceInfo.setBaseDeviceInfo(mBulbInfo);
                //todo 1.调远程接口 修改亮度值
                //2.同步cardView UI
                mAdapter.removeAt(mPosition);
                mAdapter.addData(mPosition, mDeviceInfo);
                mAdapter.notifyItemChanged(mPosition);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    //判断值是否在该范围呢
    public static boolean rangeInDefined(int current, int min, int max) {
        return Math.max(min, current) == Math.min(current, max);
    }


    private void initBulbScenesView() {
//        setContentView(R.layout.layout_bulb_inf_dialog);
        mRrecyclerView = findViewById(R.id.bulb_scene_recyclerview);
        mRrecyclerView.setHasFixedSize(true); //设置固定大小
        //TODO 这里的间隔距离需要确定
        AverageGapItemDecoration itemDecoration =
                new AverageGapItemDecoration(30, 30, 10);
        mRrecyclerView.addItemDecoration(itemDecoration);
        //创建线性布局
        //TODO 添加假数据
        dataList = initData();
        myAdapter = new BulbScenesRecyclerAdapter(R.layout.layout_bulb_scene_type, dataList); //初始化适配器
//        myAdapter.setNewInstance(dataList);
        GridLayoutManager girdLayoutManager = new GridLayoutManager(getContext(), 4);
        mRrecyclerView.setLayoutManager(girdLayoutManager);
        mRrecyclerView.setAdapter(myAdapter);
        myAdapter.setEmptyView(R.layout.layout_empty);    //设置未发现设备时的布局
    }


    private ArrayList<String> initData() {
        dataList = new ArrayList<>();
        for (int i = 0; i < 21; i++) {
            int index = i + 1;
            dataList.add("item" + index);
        }
//        dataList =null;
        return dataList;
    }

    @Override
    public void onClick(View view) {

    }
}
